package com.music.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.anmusic.application.R;
	  
	public class LucharActivity extends Activity implements OnClickListener, OnPageChangeListener{
		
		private final String GridPostion1="0x1";
		private final String GridPostion2="0x2";
		private final String GridPostion3="0x3";
		private final String GridPostion4="0x4";
		private final String GridPostion5="0x5";
		private final String GridPostion6="0x6";
	      
	    private ViewPager vp;  
	    private List<View> views;  
	    private LinearLayout mLinear;
	      
	    //引导图片资源  
	    private static final int[] pics = { R.drawable.luchar1,  
	            R.drawable.luchar1, R.drawable.luchar1,  
	            R.drawable.luchar1 };  
	      
	    //底部小店图片  
	    private ImageView[] dots ;  
	      
	    //记录当前选中位置  
	    private int currentIndex;  
	    
	    Handler handler=new Handler(){
           
			@Override
			public void handleMessage(Message msg) {
				startActivity(new Intent(LucharActivity.this,MainActivity.class));
				LucharActivity.this.finish();
			}
	    	
	    };
	    
	      
	    /** Called when the activity is first created. */  
	    @Override  
	    public void onCreate(Bundle savedInstanceState) { 
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        SetData();
	        setContentView(R.layout.luchar_main);  
	        vp = (ViewPager) findViewById(R.id.main_viewPager); 
	        mLinear=(LinearLayout)findViewById(R.id.luchar_pric);
	        views = new ArrayList<View>();  
	        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,  
	                LinearLayout.LayoutParams.MATCH_PARENT);  
	          
	        //初始化引导图片列表  
	        for(int i=0; i<pics.length; i++) {  
	            ImageView iv = new ImageView(this);
	            iv.setLayoutParams(mParams);
	            iv.setScaleType(ScaleType.FIT_XY);
	            iv.setImageResource(pics[i]);
	            //iv.setBackgroundResource(pics[i]);  
	            views.add(iv);  
	        }  
	        //初始化Adapter  
	        LucharViewpagerAdapter  vpAdapter = new LucharViewpagerAdapter();  
	        vp.setAdapter(vpAdapter);  
	        //绑定回调  
	        vp.setOnPageChangeListener(this);  
	          
	        //初始化底部小点  
	        initDots();  
	          
	    }  
	      
	    private void initDots() {  
	  
	       dots = new ImageView[pics.length];  
	        //循环取得小点图片  
	       LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
	       layoutParams.setMargins(15,0,0,0);
	        for (int i = 0; i < pics.length; i++) {  
	            dots[i] =new ImageView(this);
	            dots[i].setLayoutParams(layoutParams);
	            dots[i].setBackgroundResource(R.drawable.luchar_dots);
	            dots[i].setEnabled(true);//都设为灰色  
	           // dots[i].setOnClickListener(this);  
	            dots[i].setTag(i);//设置位置tag，方便取出与当前位置对应  
	            mLinear.addView(dots[i]);
	        }  
	       
	        currentIndex = 0;  
	        dots[currentIndex].setEnabled(false);//设置为白色，即选中状态  
	    }  
	      
	    /** 
	     *设置当前的引导页  
	     */  
	    private void setCurView(int position)  
	    {  
	        if (position < 0 || position >= pics.length) {  
	            return;  
	        } 
	        
	        vp.setCurrentItem(position); 
	        if(position==(pics.length-1)){
	        	handler.sendEmptyMessageDelayed(0, 2000);
	        }
	    }  
	  
	    /** 
	     *这只当前引导小点的选中  
	     */  
	    private void setCurDot(int positon)  
	    {  
	        if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {  
	            return;  
	        }  
	  
	        dots[positon].setEnabled(false);  
	        dots[currentIndex].setEnabled(true);  
	  
	        currentIndex = positon; 
	        if(positon==(pics.length-1)){
	        	handler.sendEmptyMessageDelayed(0, 3000);
	        }
	    }  
	  
	    //当滑动状态改变时调用  
	    @Override  
	    public void onPageScrollStateChanged(int arg0) {  
	        // TODO Auto-generated method stub  
	          
	    }  
	  
	    //当当前页面被滑动时调用  
	    @Override  
	    public void onPageScrolled(int arg0, float arg1, int arg2) {  
	        // TODO Auto-generated method stub  
	          
	    }  
	  
	    //当新的页面被选中时调用  
	    @Override  
	    public void onPageSelected(int arg0) {  
	        //设置底部小点选中状态  
	        setCurDot(arg0);  
	    }  
	  
	    //点击事件跳转
	    @Override  
	    public void onClick(View v) {  
//	        int position = (Integer)v.getTag();  
//	        setCurView(position);  
//	        setCurDot(position);  
	    }  
	    
	   class LucharViewpagerAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			
			return views.size();
		}
        
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(views.get(position));
		}


		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(views.get(position));
		
			return views.get(position);
		}


		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			
			return arg0==arg1;
		}
		   
	   }
	   
	   private void SetData(){
		    SharedPreferences sharedPreferences= getSharedPreferences("MyGridPoint", Context.MODE_PRIVATE);
	        Editor editor=sharedPreferences.edit(); 
	        editor.putInt(GridPostion1, 1);
	        editor.putInt(GridPostion2, 2);
	        editor.putInt(GridPostion3, 3);
	        editor.putInt(GridPostion4, 4);
	        editor.putInt(GridPostion5, 5);
	        editor.putInt(GridPostion6, 6);
	        editor.commit();
	   }
	}  

